package com.chuangjiangx.member.stored.ddd.domain.model;

import com.chuangjiangx.dddbase.Repository;
import com.chuangjiangx.domain.shared.model.Enable;
import com.chuangjiangx.member.stored.dao.mapper.InMbrRechargeRuleMapper;
import com.chuangjiangx.member.stored.dao.model.InMbrRechargeRule;
import com.chuangjiangx.member.stored.dao.model.InMbrRechargeRuleExample;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


@Component
public class RechargeRuleRepository implements Repository<MbrRechargeRule, MbrRechargeRuleId> {

    @Autowired
    private InMbrRechargeRuleMapper inMbrRechargeRuleMapper;


    @Override
    public MbrRechargeRule fromId(MbrRechargeRuleId mbrRechargeRuleId) {
        InMbrRechargeRule in = inMbrRechargeRuleMapper.selectByPrimaryKey(mbrRechargeRuleId.getId());
        if (in == null) {
            return null;
        }
        MbrRechargeRule mbrRechargeRule =  new MbrRechargeRule(
                in.getName(),
                in.getAmount(),
                in.getGiftScore(),
                in.getGiftAmount(),
                in.getCouponNumber(),
                in.getGiftCoupon(),
                Enable.getEnable(in.getEnable()),
                IsDelete.getDelete(in.getDel()),
                in.getCreateTime(),
                in.getUpdateTime(),
                new MerchantId(in.getMerchantId()),
                MbrGiftType.getGiftType(in.getGiftType())
        );
        mbrRechargeRule.setId(new MbrRechargeRuleId(in.getId()));
        return mbrRechargeRule;
    }

    @Override
    public void update(MbrRechargeRule mbrRechargeRule) {
        InMbrRechargeRule inMemberRechargeRule = new InMbrRechargeRule();
        inMemberRechargeRule.setId(mbrRechargeRule.getId().getId());
        inMemberRechargeRule.setName(mbrRechargeRule.getName());
        inMemberRechargeRule.setAmount(mbrRechargeRule.getAmount());
        inMemberRechargeRule.setGiftScore(mbrRechargeRule.getGiftScore());
        inMemberRechargeRule.setGiftAmount(mbrRechargeRule.getGiftAmount());
        inMemberRechargeRule.setCouponNumber(mbrRechargeRule.getCouponNumber());
        inMemberRechargeRule.setGiftCoupon(mbrRechargeRule.getGiftCoupon());
        inMemberRechargeRule.setEnable(mbrRechargeRule.getEnable() == null ? null : mbrRechargeRule.getEnable().value);
        inMemberRechargeRule.setDel(mbrRechargeRule.getIsDelete() == null ? null : mbrRechargeRule.getIsDelete().value);
        inMemberRechargeRule.setUpdateTime(new Date());
        inMemberRechargeRule.setMerchantId(mbrRechargeRule.getMerchantId().getId());
        inMemberRechargeRule.setGiftType(mbrRechargeRule.getMbrGiftType() == null ? null : mbrRechargeRule.getMbrGiftType().value);

        inMbrRechargeRuleMapper.updateByPrimaryKeySelective(inMemberRechargeRule);

    }

    @Override
    public void save(MbrRechargeRule mbrRechargeRule) {
        InMbrRechargeRule inMemberRechargeRule = new InMbrRechargeRule();
        inMemberRechargeRule.setName(mbrRechargeRule.getName());
        inMemberRechargeRule.setAmount(mbrRechargeRule.getAmount());
        inMemberRechargeRule.setGiftScore(mbrRechargeRule.getGiftScore());
        inMemberRechargeRule.setGiftAmount(mbrRechargeRule.getGiftAmount());
        inMemberRechargeRule.setCouponNumber(mbrRechargeRule.getCouponNumber());
        inMemberRechargeRule.setGiftCoupon(mbrRechargeRule.getGiftCoupon());
        inMemberRechargeRule.setEnable(mbrRechargeRule.getEnable() == null ? null : mbrRechargeRule.getEnable().value);
        inMemberRechargeRule.setDel(mbrRechargeRule.getIsDelete() == null ? null : mbrRechargeRule.getIsDelete().value);
        inMemberRechargeRule.setCreateTime(mbrRechargeRule.getCreateTime());
        inMemberRechargeRule.setUpdateTime(mbrRechargeRule.getUpdateTime());
        inMemberRechargeRule.setMerchantId(mbrRechargeRule.getMerchantId().getId());
        inMemberRechargeRule.setGiftType(mbrRechargeRule.getMbrGiftType() == null ? null : mbrRechargeRule.getMbrGiftType().value);
        inMbrRechargeRuleMapper.insertSelective(inMemberRechargeRule);
        mbrRechargeRule.setId(new MbrRechargeRuleId(inMemberRechargeRule.getId()));
    }


    /**
     * 查询商户下启用的储值规则
     *
     * @param merchantId
     * @return
     */
    public int countByMerchantId(MerchantId merchantId) {
        InMbrRechargeRuleExample example = new InMbrRechargeRuleExample();
        example.createCriteria()
                .andMerchantIdEqualTo(merchantId.getId())
                .andEnableEqualTo(Enable.ENABLE.value)
                .andDelEqualTo(IsDelete.NOTDELETE.value);
        List<InMbrRechargeRule> list = inMbrRechargeRuleMapper.selectByExample(example);
        return list.size();
    }

    public int rechargeRulesByName(String name, MerchantId merchantId) {
        InMbrRechargeRuleExample example = new InMbrRechargeRuleExample();
        example.createCriteria()
                .andMerchantIdEqualTo(merchantId.getId())
                .andNameEqualTo(name)
                .andDelEqualTo(IsDelete.NOTDELETE.value);
        List<InMbrRechargeRule> list = inMbrRechargeRuleMapper.selectByExample(example);
        return list.size();
    }

    /**
     * 根据商户和规则id查询
     *
     * @param merchantId
     * @param mbrRechargeRuleId
     * @return
     */
    public MbrRechargeRule selectrechargeRulesByMerchantIdAndStoredRulesId(MerchantId merchantId, MbrRechargeRuleId mbrRechargeRuleId) {
        InMbrRechargeRuleExample example = new InMbrRechargeRuleExample();
        example.createCriteria()
                .andMerchantIdEqualTo(merchantId.getId())
                .andIdEqualTo(mbrRechargeRuleId.getId());
        List<InMbrRechargeRule> list = inMbrRechargeRuleMapper.selectByExample(example);
        InMbrRechargeRule inMemberRechargeRule = list.get(0);
        MbrRechargeRule mbrRechargeRule = new MbrRechargeRule();
        BeanUtils.copyProperties(inMemberRechargeRule, mbrRechargeRule);
        mbrRechargeRule.setEnable(Enable.getEnable(inMemberRechargeRule.getEnable()));
        mbrRechargeRule.setIsDelete(IsDelete.getDelete(inMemberRechargeRule.getDel()));
        mbrRechargeRule.setMerchantId(new MerchantId(inMemberRechargeRule.getMerchantId()));
        mbrRechargeRule.setMbrGiftType(MbrGiftType.getGiftType(inMemberRechargeRule.getGiftType()));
        mbrRechargeRule.setId(new MbrRechargeRuleId(inMemberRechargeRule.getId()));
        return mbrRechargeRule;
    }
}
